package com.ruoyi.project.cms.type.service;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.cms.type.domain.CMSType;
import com.ruoyi.project.cms.type.mapper.CMSTypeMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CMSTypeServiceImpl implements  ICMSTypeService {

    @Resource
    private CMSTypeMapper cmsTypeMapper;

    @Override
    public List<CMSType> selectCMSTypeList(CMSType cmsType) {
        return cmsTypeMapper.selectCMSTypeList(cmsType);
    }

    @Override
    public String checkTypeNameUnique(CMSType cmsType) {

        //判断前端是新增还是修改，如果是新增typeId 设置-1，修改直接获取id
        Long typeId = StringUtils.isNull(cmsType.getTypeId()) ? -1L : cmsType.getTypeId();

        CMSType info = cmsTypeMapper.selectCMSTypeBYTypeName(cmsType.getTypeName());
        System.out.println("info :"+info);
        if (StringUtils.isNotNull(info) && info.getTypeId().longValue() != typeId.longValue())
        {
            return UserConstants.CMS_TYPE_NAME_NOT_UNIQUE;
        }
        return UserConstants.CMS_TYPE_NAME_UNIQUE;
    }

    @Override
    public int insertCMSType(CMSType cmsType) {

        //设置创建人
        cmsType.setCreateBy(ShiroUtils.getLoginName());

        return cmsTypeMapper.insertCMSType(cmsType);
    }

    @Override
    public CMSType sleectByCMSTypeId(Long typeId) {
        return cmsTypeMapper.selectByCMSTypeId(typeId);
    }

    @Override
    public int updateCMSType(CMSType cmsType) {

        //设置修改人
        cmsType.setUpdateBy(ShiroUtils.getLoginName());

        return cmsTypeMapper.updateById(cmsType);
    }

    @Override
    public int deleteByIds(Long[] ids) {
        //将字符串转换成数组

        return cmsTypeMapper.deletetypeByIds(ids);
    }
}
